<?php if(navigation::getAdminAction() === 'create' || navigation::getAdminAction() === 'update' ): ?>
<?php
if(navigation::getAdminModeleId()>0){
    $category = new modele_tag(navigation::getAdminModeleId());
    $data = $category->getDatas();
    $data['permalien'] = $category->getPermalien()->getCleanedLien(false);
    $action = 'update';
}else{
    $action = 'create';
    $data = array();
}
?><div class="columns thirteen">
    <h1><a class="pwe-ui-btn" href="?template=tag"><span class="ui-icon ui-icon ui-icon-arrowthick-1-w"></span></a> Modifier/créer une rubrique</h1>
    <hr style="margin: 5px 0 15px 0;">
</div>
<form method="post" enctype="multipart/form-data" action="?template=tag&action=<?php echo $action; ?>&modele=tag&do=1&modele_id=<?php echo isset($data['id'])?$data['id']:0; ?>">
    <div class="columns nine">
        <div align="right">
            <input type="submit" value="Enregistrer" class="pwe-ui-btn"/> 
            <?php if(isset($data['id']) && $data['id']>0): ?><a href="<?php echo $data['url']; ?>" target="_blank" title="voir" class="pwe-ui-btn">voir</a><?php endif; ?>
        </div>
        <?php echo $this->getInputHtml('Titre', 'modele_title', (isset($data['title']))?$data['title']:'', 'tag', (isset($data['id']))?$data['id']:0, array('class_input'=>'titreModele','actions'=>array('onblur'=>"javascript:generatePermalien($(this).val());"))); ?>
        <?php echo $this->getInputHtml('Permalien', 'modele_lien', (isset($data['permalien']))?$data['permalien']:'', 'tag', (isset($data['id']))?$data['id']:0, array('prefix'=>PWE::getBaseURL(),'sufix'=>modele_permalien::getExtensionByModele('category'),'id_input'=>'permalien','actions'=>array('onblur'=>"javascript:validPermalien($(this).val());"))); ?>
        
        <?php include('inc/seoManager.inc.php'); ?>
        <?php include('inc/redirectionManager.inc.php'); ?>
        <div align="right">
            <input type="submit" value="Enregistrer" class="pwe-ui-btn"/> 
            <?php if(isset($data['id']) && $data['id']>0): ?><a href="<?php echo $data['url']; ?>" target="_blank" title="voir" class="pwe-ui-btn">voir</a><?php endif; ?>
        </div>
    </div>
    <div class="columns four">
        <?php include('inc/templateManager.inc.php'); ?>
        <br/><br/>
        <?php include('inc/mediaManager.inc.php'); ?>
        <input type="hidden" name="modele_id" value="<?php echo (isset($data['id']))?$data['id']:0; ?>" />
    </div>
</form>
<?php else: ?>
<?php
// enregistrer le changement d'ordre
if(isset($_POST) && isset($_POST['save_order'])){
    foreach($_POST as $var=>$value){
        if(substr($var,0,6)==='order_'){
            list($s, $idp) = explode('_',$var,2);
            $t = new modele_tag($idp);
            $t->setOrder($value);
        }
    }
}

$liste = modele_tag::getPrimaryListe();
$sortable = true;
?>

<div class="columns thirteen">
<h1>Liste des Tags</h1>
<hr style="margin: 5px 0 25px 0;">
    <div align="right">
        <a class="pwe-ui-btn" href="?template=tag&modele=tag&action=create">Créer un tag</a>
    </div><br/>
<?php if($sortable): ?><form method="post"><?php endif; ?>
    <table class="liste ui-widget<?php if($sortable): ?> table_sortable<?php endif; ?>" width="100%">
        <thead class="ui-widget-header">
            <tr>
                <th></th>
                <th>Nom</th>
                <th>Permalien</th>
                <th>Articles</th>
                <th>Modèle</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody<?php if($sortable): ?> class="liste-items-menu"<?php endif; ?>>
            <?php foreach($liste as $i=>$tag): ?>
            <?php $data = $tag->getDatas(); ?>
            <tr<?php if($sortable): ?> style="cursor: move;"<?php endif; ?>>
                <td>
                    <?php echo $i+1; ?>
                    <?php if($sortable): ?>
                    <input type="hidden" class="orderValue" name="order_<?php echo $tag->getId(); ?>" value="<?php echo $i+1; ?>" />
                    <?php endif; ?>
                </td>
                <td>
                    <span><?php echo $data['title']; ?></span><br/>
                </td>
                <td><?php echo $data['permalien'] ?></td>
                <td style="text-align: center;">
                    <?php echo $tag->getNombrePublications(); ?>
                    <a class="pwe-ui-btn" href="?template=publication&modele=publication&tag_id=<?php echo $data['id']; ?>" title="Voir la liste"><span class="ui-icon ui-icon-folder-open"></span></a>
                </td>
                <td><?php echo $data['template']; ?></td>
                <td>
                    <a class="pwe-ui-btn" href="<?php echo $data['url']; ?>" target="preview_site" title="Voir"><span class="ui-icon ui-icon-extlink"></span></a>
                    <a class="pwe-ui-btn" href="?template=tag&modele=tag&action=update&modele_id=<?php echo $data['id']; ?>" title="Modifier"><span class="ui-icon ui-icon-pencil"></span></a>
                    <a href="?template=tag&modele=tag&action=delete&do=1&modele_id=<?php echo $data['id']; ?>" class="confirmAction pwe-ui-btn" title="Supprimer le tag '<?php echo $data['title']; ?>'"><span class="ui-icon ui-icon-trash"></span></a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot></tfoot>
    </table>
<?php if($sortable): ?>
    <br/><div align="right"><br/>(modifier l'ordre en déplaçant les lignes par glisser/déposer) <input class="pwe-ui-btn" type="submit" name="save_order" value="enregistrer l'ordre" /></div>
</form>
<?php endif; ?>
</div>
<?php endif; ?>